<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>创建课程-课程详情</title>
	<link rel="stylesheet" href="css/left.css">
	<link rel="stylesheet" href="css/addcourse_details.css">
	<style>
		a.input {
   			display: inline-block;/*具有行内元素的视觉，块级元素的属性 宽高*/
   			overflow: hidden;/*去掉的话，输入框也可以点击*/
   			position: relative;/*相对定位，为 #file 的绝对定位准备*/
   			left: -3px;
   			font-size: 15px;
   			font-weight: 600;
   			text-decoration: none;
   			color: #9e9d9d
 		} 

 		a.input:hover {
  			text-decoration: underline;
 		}

 		#upfile,#infile {
   			opacity: 0;/*设置此控件透明度为零，即完全透明*/
   			filter: alpha(opacity=0);/*设置此控件透明度为零，即完全透明针对IE*/
   			font-size: 100px;
   			position: absolute;/*绝对定位，相对于 .input */
   			top: 0;
   			right: 10px;
 		}
    	#image-holder {
        	width: 124px;
        	height: 106px;
        	border-radius: 50%;
        	border: 1px solid #fff;
        	display: inline-block;
    	}
    	.thumb-image {
        	width: 100%;
        	height: 100%;
    	}
	</style>
</head>
<body>
<!-- 加载头部信息 -->
<?php
	include_once("header.php");
?>

<!-- 中间内容 -->
<div class="content clearfloat">

	<!-- 右边内容 -->
	<div class="center_content">
		<div class="center_info">
			<div class="course_form">
				<form action="" method="post" class="select_img">
					<label for="btn_img">封面图片:</label>					
<?php
$file_img = "images/no_image.jpg";

if(isset($_COOKIE["tea_number"])) {

	$username = $_COOKIE["tea_number"];

	$selecttea = "select course_img from courses where tea_number='$username'";
	$result = mysql_query($selecttea) or die("查询老师信息表头像失败：".mysql_error()."<br/>");

	$row = mysql_fetch_array($result,MYSQL_ASSOC);

	$file_img = $row["course_img"];

	if($file_img == NULL) {
		$file_img = "images/no_image.jpg";
	} else {
		$file_img = $row["course_img"];
	}
}
?>
					<div class="img"><?php echo "<img src='{$file_img}' width='124' height='106' id='file_img'";?></div>
					<a href="javascript:void(0);" class="input btn">
						选择封面<input type="file" id="upfile" name="upfile">
					</a>&nbsp;&nbsp;&nbsp;&nbsp;
				</form>
			</div>
		</div>
	</div>
	
	
</div>

<?php 
if(isset($_COOKIE["tea_number"])) {
	$tea_number = $_COOKIE["tea_number"];

	if(isset($_POST["submit_down"]) && $_POST["submit_down"] == "点击下一步") {

		foreach($_FILES as $key => $value) {
			// echo "{$key}的值为:<br/>";
			foreach ($_FILES[$key] as $ke => $valu) {
				// echo "{$ke}: {$valu}<br/>";
			}
		}

		if(!empty($_FILES[$key]["name"])) {//判断是否选择了文件上传
			//指定上传文件路径以及名字
			$pathname = "./course_imgs/".$_FILES[$key]["name"];

			$course_img = "course_imgs/".$_FILES[$key]["name"];
		
			if(file_exists($pathname)) {
				//服务器上文件存在
				echo "<script>alert('文件存在!');</script>";
			} else {
				//文件不存在可以上传
				$result = move_uploaded_file($_FILES[$key]["tmp_name"], $pathname);					
			}
		} else {
			echo "<script>alert('请选择上传文件上传!');</script>";

			$course_img = "images/no_image.jpg";

			if($course_img == "images/no_image.jpg" || $tea_touxiang == "course_imgs/") {
				$course_img = "images/no_image.jpg";
			} else {
				$course_img = "course_imgs/".$_FILES[$key]["name"];
			}
		}

		$course_credit = $_POST["course_credit"];
		$course_period = $_POST["course_period"];
		$course_type = $_POST["course_type"];
		$summary = $_POST["summary"];

		$updatesql = "update courses set course_img='$course_img',course_credit='$course_credit',
			course_period='$course_period',course_type='$course_type',course_summary='$summary'
			where tea_number='$tea_number'";
		
		@mysql_query($updatesql) or die("修改课程信息表失败：".mysql_error()."<br/>");
	
		$selectcourse = "select course_img from courses where tea_number='$tea_number'";
		$result = mysql_query($selectcourse) or die("查询课程信息表图片失败：".mysql_error()."<br/>");

		$row = mysql_fetch_array($result,MYSQL_ASSOC);

		$img = $row["course_img"];

		echo <<<script_img
		<script>
			var file_img = document.getElementById("file_img");
			file_img.src = "{$img}"; 
		</script>
script_img;


		echo "<script>";
		echo "{window.location='addcourse_finish.php';}";
		echo "</script>";
	}

	/*if(isset($_POST["submit"]) && $_POST["submit"] == "取消") {
		echo "<script>";
		echo "{window.location='addcourse.php';}";
		echo "</script>";
	}*/
}
?>

<script src="js/jquery-3.2.1.js"></script>
<script>
    $("#upfile").on('change', function () {
        if (typeof (FileReader) != "undefined") {

            var image_holder = $("#image-holder");
            image_holder.empty();

            var reader = new FileReader();
            reader.onload = function (e) {
                $("<img />", {
                    "src": e.target.result,
                    "class": "thumb-image"
                }).appendTo(image_holder);
            };
            image_holder.show();
            reader.readAsDataURL($(this)[0].files[0]);
        } else {
            alert("你的浏览器不支持FileReader.");
        }

    });
</script>

<!-- 加载底部信息 -->
<?php
	// include_once("footer.php");
?>
</body>
</html>